<!DOCTYPE html>
<pre id="log"></pre>

<div id="ltrTextContainer" contenteditable>foo bar <span contenteditable=false>baz</span> qux quux</div>
<div id="rtlTextContainer" contenteditable dir="rtl">&#x05E9;&#x05D5;&#x05BC;&#x05E8;&#x05D4; &#x05E9;&#x05D5;&#x05BC;&#x05E8;&#x05D4;
<span contenteditable=false>&#x05E9;&#x05D5;&#x05BC;&#x05E8;&#x05D4;</span>
&#x05E9;&#x05D5;&#x05BC;&#x05E8;&#x05D4; &#x05E9;&#x05D5;&#x05BC;&#x05E8;&#x05D4;</div>

<div id="ltrMultiTextContainer" contenteditable>foo bar <span contenteditable=false>baz baz baz</span> qux quux</div>
<div id="rtlMultiTextContainer" contenteditable dir="rtl">&#x05E9;&#x05D5;&#x05BC;&#x05E8;&#x05D4; &#x05E9;&#x05D5;&#x05BC;&#x05E8;&#x05D4;
<span contenteditable=false>&#x05E9;&#x05D5;&#x05BC;&#x05E8;&#x05D4; &#x05E9;&#x05D5;&#x05BC;&#x05E8;&#x05D4; &#x05E9;&#x05D5;&#x05BC;&#x05E8;&#x05D4;</span>
&#x05E9;&#x05D5;&#x05BC;&#x05E8;&#x05D4; &#x05E9;&#x05D5;&#x05BC;&#x05E8;&#x05D4;</div>

<script>
function log(s) {
  document.getElementById("log").appendChild(document.createTextNode(s + "\n"));
}

function placeCursorBeforeFirstNoneditableChild(container) {
  for (var i = 0; i < container.childNodes.length; i++) {
    var node = container.childNodes[i];
    if (node.isContentEditable !== undefined && !node.isContentEditable) {
      var previous = node.previousSibling;
      getSelection().collapse(previous, previous.textContent.length);
      return node;
    }
  }
  throw "Couldn't find noneditable child of " + container.textContent;
}

function extendForwardByWord(container, bidiName) {
  noneditableChild = placeCursorBeforeFirstNoneditableChild(container);
  getSelection().modify("extend", "forward", "word");
  if (getSelection().toString() === noneditableChild.textContent)
    log("PASS for " + bidiName);
  else
    log("FAIL for " + bidiName + ", selection is \"" + getSelection() + "\" but should be \"" + noneditableChild.textContent + "\"");
}

if (window.internals)
    internals.settings.setEditingBehavior("mac");
extendForwardByWord(document.getElementById("ltrTextContainer"), "LTR");
extendForwardByWord(document.getElementById("rtlTextContainer"), "RTL");
extendForwardByWord(document.getElementById("ltrMultiTextContainer"), "LTR-multi");
extendForwardByWord(document.getElementById("rtlMultiTextContainer"), "RTL-multi");

if (window.testRunner)
    testRunner.dumpAsText();
</script>
